﻿namespace SERFLITEMAX20.WinUI.Controls
{
    using SERFLITEMAX20;
    using SERFLITEMAX20.Core;
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class MyObjectList : UserControl
    {
        private ColumnHeader chGUID;
        private ColumnHeader chName;
        private ColumnHeader chType;
        private ColumnHeader columnHeader1;
        private ColumnHeader columnHeader2;
        private ColumnHeader columnHeader3;
        private ColumnHeader columnHeader4;
        private ColumnHeader columnHeader5;
        private ColumnHeader columnHeader6;
        private ColumnHeader columnHeader7;
        private ColumnHeader columnHeader8;
        private IContainer components = null;
        private Label lblCaption;
        private ListView lstView;
        private Hashtable mItems = new Hashtable();
        private ListViewSortManager mListViewSortManager = null;
        private Main mMain = null;
        private ContextMenuStrip mnuMain;
        private ToolStripMenuItem mnuMainSetColor;
        private ToolStripMenuItem mnuMainSetFont;
        private enObjectTypes mType;
        private ColorDialog PickColor;
        private FontDialog PickFont;
        private delUpdateViewer tdel = null;
        private delclear tdelclear = null;

        public MyObjectList()
        {
            this.InitializeComponent();
        }

        public void Clear()
        {
            if (this.tdelclear == null)
            {
                this.tdelclear = new delclear(this.ClearThread);
            }
            base.Invoke(this.tdelclear);
        }

        private void ClearThread()
        {
            this.lstView.Items.Clear();
            this.mItems = new Hashtable();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.lstView = new System.Windows.Forms.ListView();
            this.chGUID = new System.Windows.Forms.ColumnHeader();
            this.chType = new System.Windows.Forms.ColumnHeader();
            this.chName = new System.Windows.Forms.ColumnHeader();
            this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
            this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
            this.mnuMain = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.mnuMainSetColor = new System.Windows.Forms.ToolStripMenuItem();
            this.mnuMainSetFont = new System.Windows.Forms.ToolStripMenuItem();
            this.PickColor = new System.Windows.Forms.ColorDialog();
            this.PickFont = new System.Windows.Forms.FontDialog();
            this.lblCaption = new System.Windows.Forms.Label();
            this.mnuMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // lstView
            // 
            this.lstView.CheckBoxes = true;
            this.lstView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.chGUID,
            this.chType,
            this.chName,
            this.columnHeader1,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4,
            this.columnHeader5,
            this.columnHeader6,
            this.columnHeader8,
            this.columnHeader7});
            this.lstView.ContextMenuStrip = this.mnuMain;
            this.lstView.Dock = System.Windows.Forms.DockStyle.Fill;
            this.lstView.FullRowSelect = true;
            this.lstView.GridLines = true;
            this.lstView.HideSelection = false;
            this.lstView.Location = new System.Drawing.Point(0, 16);
            this.lstView.Name = "lstView";
            this.lstView.Size = new System.Drawing.Size(551, 275);
            this.lstView.TabIndex = 0;
            this.lstView.UseCompatibleStateImageBehavior = false;
            this.lstView.View = System.Windows.Forms.View.Details;
            this.lstView.SelectedIndexChanged += new System.EventHandler(this.lstView_SelectedIndexChanged);
            this.lstView.DoubleClick += new System.EventHandler(this.lstView_DoubleClick);
            this.lstView.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lstView_KeyDown);
            // 
            // chGUID
            // 
            this.chGUID.Text = "GUID";
            this.chGUID.Width = 61;
            // 
            // chType
            // 
            this.chType.Text = "Type";
            this.chType.Width = 107;
            // 
            // chName
            // 
            this.chName.Text = "Name";
            this.chName.Width = 143;
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "X";
            // 
            // columnHeader2
            // 
            this.columnHeader2.Text = "Y";
            // 
            // columnHeader3
            // 
            this.columnHeader3.Text = "Z";
            // 
            // columnHeader4
            // 
            this.columnHeader4.Text = "FACING";
            // 
            // columnHeader5
            // 
            this.columnHeader5.Text = "SPEED";
            // 
            // columnHeader6
            // 
            this.columnHeader6.Text = "Flags";
            // 
            // columnHeader8
            // 
            this.columnHeader8.Text = "DynFlags";
            // 
            // columnHeader7
            // 
            this.columnHeader7.Text = "Reaction";
            // 
            // mnuMain
            // 
            this.mnuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuMainSetColor,
            this.mnuMainSetFont});
            this.mnuMain.Name = "mnuMain";
            this.mnuMain.Size = new System.Drawing.Size(130, 48);
            // 
            // mnuMainSetColor
            // 
            this.mnuMainSetColor.Name = "mnuMainSetColor";
            this.mnuMainSetColor.Size = new System.Drawing.Size(129, 22);
            this.mnuMainSetColor.Text = "Set Color";
            // 
            // mnuMainSetFont
            // 
            this.mnuMainSetFont.Name = "mnuMainSetFont";
            this.mnuMainSetFont.Size = new System.Drawing.Size(129, 22);
            this.mnuMainSetFont.Text = "Set Font";
            // 
            // lblCaption
            // 
            this.lblCaption.Dock = System.Windows.Forms.DockStyle.Top;
            this.lblCaption.Location = new System.Drawing.Point(0, 0);
            this.lblCaption.Name = "lblCaption";
            this.lblCaption.Size = new System.Drawing.Size(551, 16);
            this.lblCaption.TabIndex = 1;
            this.lblCaption.DoubleClick += new System.EventHandler(this.lblCaption_DoubleClick);
            // 
            // MyObjectList
            // 
            this.Controls.Add(this.lstView);
            this.Controls.Add(this.lblCaption);
            this.Name = "MyObjectList";
            this.Size = new System.Drawing.Size(551, 291);
            this.mnuMain.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        private void InitListViewSorter()
        {
            this.lstView.ListViewItemSorter = null;
            this.mListViewSortManager = new ListViewSortManager(this.lstView, new System.Type[] { typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort), typeof(ListViewTextSort) });
        }

        private void lblCaption_DoubleClick(object sender, EventArgs e)
        {
            this.ClearThread();
        }

        public void LoadCol(Main pMain, enObjectTypes pType, bool pView)
        {
            if (pMain != null)
            {
                this.mMain = pMain;
                this.mType = pType;
                if (pView)
                {
                    if (this.mListViewSortManager == null)
                    {
                        this.InitListViewSorter();
                    }
                    this.UpdateViewer();
                }
                else
                {
                    this.Clear();
                }
            }
        }

        private void LoadGameObject(GameObject pObject)
        {
            ListViewItem item;
            if (this.mItems.Contains(pObject.Guid))
            {
                item = (ListViewItem) this.mItems[pObject.Guid];
                item.Tag = pObject;
                item.SubItems[1].Text = pObject.TypeId.ToString();
                item.SubItems[2].Text = pObject.Name;
                item.SubItems[3].Text = pObject.X.ToString();
                item.SubItems[4].Text = pObject.Y.ToString();
                item.SubItems[5].Text = pObject.Z.ToString();
                item.SubItems[6].Text = pObject.Facing.ToString();
                item.SubItems[7].Text = "";
                item.SubItems[8].Text = pObject.Flags.ToString();
                item.SubItems[9].Text = pObject.DynFlags.ToString();
                item.SubItems[10].Text = "";
            }
            else
            {
                item = this.lstView.Items.Add(((ulong) pObject.Guid).ToString());
                this.mItems.Add(pObject.Guid, item);
                item.Tag = pObject;
                item.SubItems.Add(pObject.TypeId.ToString());
                item.SubItems.Add(pObject.Name);
                item.SubItems.Add(pObject.X.ToString());
                item.SubItems.Add(pObject.Y.ToString());
                item.SubItems.Add(pObject.Z.ToString());
                item.SubItems.Add(pObject.Facing.ToString());
                item.SubItems.Add("");
                item.SubItems.Add(pObject.Flags.ToString());
                item.SubItems.Add(pObject.DynFlags.ToString());
                item.SubItems.Add("");
            }
        }

        private void LoadItem(BaseObject pObject)
        {
            if (pObject is UnitBase)
            {
                this.LoadUnitBase((UnitBase) pObject);
            }
            else if (pObject is GameObject)
            {
                this.LoadGameObject((GameObject) pObject);
            }
        }

        public void LoadUnitBase(UnitBase pObject)
        {
            ListViewItem item;
            if (this.mItems.Contains(pObject.Guid))
            {
                item = (ListViewItem) this.mItems[pObject.Guid];
                item.Tag = pObject;
                item.SubItems[1].Text = "";
                item.SubItems[2].Text = pObject.Name;
                item.SubItems[3].Text = pObject.X.ToString();
                item.SubItems[4].Text = pObject.Y.ToString();
                item.SubItems[5].Text = pObject.Z.ToString();
                item.SubItems[6].Text = pObject.Facing.ToString();
                item.SubItems[7].Text = pObject.Speed.ToString();
                item.SubItems[8].Text = pObject.Flags.ToString();
                item.SubItems[9].Text = pObject.DynFlags.ToString();
                item.SubItems[10].Text = pObject.UnitReactionToPlayer.ToString();
            }
            else
            {
                item = this.lstView.Items.Add(((ulong) pObject.Guid).ToString());
                this.mItems.Add(pObject.Guid, item);
                item.Tag = pObject;
                item.SubItems.Add("");
                item.SubItems.Add(pObject.Name);
                item.SubItems.Add(pObject.X.ToString());
                item.SubItems.Add(pObject.Y.ToString());
                item.SubItems.Add(pObject.Z.ToString());
                item.SubItems.Add(pObject.Facing.ToString());
                item.SubItems.Add(pObject.Speed.ToString());
                item.SubItems.Add(pObject.Flags.ToString());
                item.SubItems.Add(pObject.DynFlags.ToString());
                item.SubItems.Add(pObject.UnitReactionToPlayer.ToString());
            }
        }

        private void lstView_DoubleClick(object sender, EventArgs e)
        {
            if (this.SelectedItem != null)
            {
                new frmListValues().View(this.mMain, this.SelectedItem);
            }
        }

        private void lstView_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && (e.KeyCode == Keys.C))
            {
                Clipboard.SetText(this.SelectedItem.Guid.ToString());
            }
        }

        private void lstView_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
            }
            catch
            {
            }
        }

        private void UpdateViewer()
        {
            foreach (BaseObject obj2 in this.mMain.Objects.Values)
            {
                if (this.mType == obj2.Type)
                {
                    this.LoadItem(obj2);
                }
            }
            Hashtable hashtable = new Hashtable();
            foreach (long num in this.mItems.Keys)
            {
                if (!this.mMain.Objects.Contains(num))
                {
                    hashtable.Add(num, null);
                }
            }
            foreach (long num in hashtable.Keys)
            {
                ((ListViewItem) this.mItems[num]).Remove();
                this.mItems.Remove(num);
            }
        }

        public BaseObject SelectedItem
        {
            get
            {
                if (this.lstView.SelectedItems.Count > 0)
                {
                    return (BaseObject) this.lstView.SelectedItems[0].Tag;
                }
                return null;
            }
        }

        private delegate void delclear();

        private delegate void delUpdateViewer();
    }
}

